package com.shgsh.server.utils;


import com.shgsh.server.message.Message;
import com.shgsh.server.model.IMMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * Created by jessy on 2015/8/15.
 */
public class DoWhatAndSendMes {
    static Logger logger = LoggerFactory.getLogger(DoWhatAndSendMes.class);

    public static void sendMes(IMMessage immessage) {
        ObjectOutputStream oos = null;
        Message message = null;
        try {
            if(ManageServerConClient.isOnline(immessage.getReceiverId())) {
                logger.debug(immessage.getSendId() +" is ready to send the message to " + immessage.getReceiverId());
                //获得接受者线程
                ServerConClientThread scc = ManageServerConClient.getClientThread(immessage.getReceiverId());
                //接受者线程输出流
                oos = new ObjectOutputStream(scc.s.getOutputStream());
                message = new Message(true,"message send successfully !",immessage);
                oos.writeObject(message);
                logger.debug(immessage.getSendId() + " send the message to " + immessage.getReceiverId() + "successfully");
            }else{
                //获得发送者线程
                ServerConClientThread scc = ManageServerConClient.getClientThread(immessage.getSendId());
                //接受者线程输出流
                oos = new ObjectOutputStream(scc.s.getOutputStream());
                message = new Message(false,"receiver is offline.");
                oos.writeObject(message);
                logger.debug(immessage.getSendId() + " send the message to " + immessage.getReceiverId() + "fail");

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
